package com.pig4cloud.pig.module.controller.mer;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.estate.Estate;
import com.pig4cloud.pig.module.api.entity.mer.MerVipOrder;
import com.pig4cloud.pig.module.api.entity.property.Property;
import com.pig4cloud.pig.module.api.entity.wx.WxUser;
import com.pig4cloud.pig.module.service.estate.IEstateService;
import com.pig4cloud.pig.module.service.mer.IMerVipOrderService;
import com.pig4cloud.pig.module.service.property.IPropertyService;
import com.pig4cloud.pig.module.service.wx.IWxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 套餐活动支付订单Controller
 *
 * @author ruoyi
 * @date 2022-10-20
 */
@Validated
@Api(value = "套餐活动支付订单控制器", tags = {"套餐活动支付订单管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/merVipOrder")
public class MerVipOrderController {

    private final IMerVipOrderService merVipOrderService;
    private final IWxUserService wxUserService;
    private final IEstateService estateService;
    private final IPropertyService propertyService;

	@Value("${ruoyi.imgUrl}")
	private String imgUrl;
    /**
     * 查询套餐活动支付订单列表
     */
    @ApiOperation("查询套餐活动支付订单列表")
    @PreAuthorize("@pms.hasPermission('module:merVipOrder:list')")
    @GetMapping("/list")
    public TableDataInfo<MerVipOrder> list(@Validated MerVipOrder merVipOrder) {
        QueryWrapper<MerVipOrder> qw = new QueryWrapper<>();
        if(merVipOrder.getOrderNo()!=null){
            qw.eq("order_no",merVipOrder.getOrderNo());
        }
        if(merVipOrder.getNickName()!=null){
            qw.like("nick_name",merVipOrder.getNickName());
        }
        if(merVipOrder.getStatus()!=null){
            qw.eq("status",merVipOrder.getStatus());
        }
        TableDataInfo<MerVipOrder> td = PageUtils.buildDataInfo(merVipOrderService.page(PageUtils.buildPage(),qw));
        List<MerVipOrder> list = td.getData();
        for (MerVipOrder list1:list){
            WxUser wxUser=wxUserService.findById(list1.getUserId());
            if(wxUser != null){
            	if(StrUtil.isNotEmpty(wxUser.getFaceUrl())){
					list1.setFaceUrl(wxUser.getFaceUrl());
					if(!wxUser.getFaceUrl().contains("http")){
						list1.setFaceUrl(imgUrl + wxUser.getFaceUrl());
					}
				}
			}

            Estate estate = estateService.findById(list1.getEstateId());
            if(estate != null){
				list1.setEstateName(estate.getEstateName());
			}

            Property property = propertyService.findById(list1.getPropertyId());
            if(property != null){
				list1.setPropertyName(property.getPropertyName());
			}
        }
        return td;
    }

    /**
     * 导出套餐活动支付订单列表
     */
    @ApiOperation("导出套餐活动支付订单列表")
    @PreAuthorize("@pms.hasPermission('module:merVipOrder:export')")
    @SysLog("套餐活动支付订单")
    @GetMapping("/export")
    public List export(@Validated MerVipOrder merVipOrder) {
        List<MerVipOrder> list = merVipOrderService.list();
        // ExcelUtil<MerVipOrder> util = new // ExcelUtil<MerVipOrder>(MerVipOrder.class);
        return  list;// util.exportExcel(list, "套餐活动支付订单");
    }

    /**
     * 获取套餐活动支付订单详细信息
     */
    @ApiOperation("获取套餐活动支付订单详细信息")
    @PreAuthorize("@pms.hasPermission('module:merVipOrder:query')")
    @GetMapping("/{id}")
    public R<MerVipOrder> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") String id) {
        MerVipOrder merVipOrder = merVipOrderService.getById(id);
        return R.ok(merVipOrder);
    }

    /**
     * 新增套餐活动支付订单
     */
    @ApiOperation("新增套餐活动支付订单")
    @PreAuthorize("@pms.hasPermission('module:merVipOrder:add')")
    @SysLog("套餐活动支付订单")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody MerVipOrder merVipOrder) {
        return R.okByRow(merVipOrderService.save(merVipOrder) ? 1 : 0);
    }

    /**
     * 修改套餐活动支付订单
     */
    @ApiOperation("修改套餐活动支付订单")
    @PreAuthorize("@pms.hasPermission('module:merVipOrder:edit')")
    @SysLog("套餐活动支付订单")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody MerVipOrder merVipOrder) {
        return R.okByRow(merVipOrderService.updateById(merVipOrder) ? 1 : 0);
    }

    /**
     * 删除套餐活动支付订单
     */
    @ApiOperation("删除套餐活动支付订单")
    @PreAuthorize("@pms.hasPermission('module:merVipOrder:remove')")
    @SysLog("套餐活动支付订单")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable String[] ids) {
        return R.okByRow(merVipOrderService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
